package com.sumover.Service.impl;

import com.sumover.DAO.*;
import com.sumover.POJO.*;
import com.sumover.Service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private NewsMapper newsMapper;

    @Autowired
    private GameStrategyMapper gameStrategyMapper;

    @Autowired
    private GameEvaluationMapper gameEvaluationMapper;

    @Autowired
    private ArticleDetailMapper articleDetailMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public Article queryArticle(Integer articleId) {
        Article resultArticle = articleMapper.selectByPrimaryKey(articleId);
        return resultArticle;
    }

    @Override
    public List<News> getAllNews() {
        List<News> newsList = newsMapper.selectAll();
        return newsList;
    }

    @Override
    public List<GameStrategy> getAllStrategy() {
        List<GameStrategy> gameStrategies = gameStrategyMapper.selectAll();
        return gameStrategies;
    }

    @Override
    public List<GameStrategy> getStrategiesByGame(Game game) {
        List<GameStrategy> gameStrategies = gameStrategyMapper.selectByGame(game);
        return gameStrategies;
    }

    @Override
    public Article getNewsArticle(News news) {
        Article result = articleMapper.selectByPrimaryKey(news.getAid());
        return result;
    }

    @Override
    public Article getStrategyArticle(GameStrategy gameStrategy) {
        Article result = articleMapper.selectByPrimaryKey(gameStrategy.getAid());
        return result;
    }

    @Override
    public Article getEvluationArticle(GameEvaluation gameEvaluation) {
        Article result = articleMapper.selectByPrimaryKey(gameEvaluation.getAid());
        return result;
    }

    @Override
    public List<GameEvaluation> getGameEvaluations(Game game) {
        List<GameEvaluation> gameEvaluation = gameEvaluationMapper.selectByGame(game);
        return gameEvaluation;
    }

    @Override
    public ArticleDetail getArticleDetail(Article article) {
        ArticleDetail articleDetail = articleDetailMapper.getArticleDetail(article);
        return articleDetail;
    }

    @Override
    public Article getArticle(Integer articleId) {
        return articleMapper.selectByPrimaryKey(articleId);
    }

    @Override
    public User getArticlePublisher(Article article) {
        User user = userMapper.selectByPrimaryKey(article.getPublisherid());
        return user;
    }

    @Override
    public String getArticleUsername(Integer aid) {
        Integer publisherid = articleMapper.selectByPrimaryKey(aid).getPublisherid();
        return userMapper.selectByPrimaryKey(publisherid).getUsername();
    }

    @Override
    public User getArticleUser(Integer aid) {
        Article article = articleMapper.selectByPrimaryKey(aid);
        User user = userMapper.selectByPrimaryKey(article.getArticleid());
        return user;
    }

    @Override
    public Article getEvaluationsArticle(GameEvaluation gameEvaluation) {
        Article article = articleMapper.selectByPrimaryKey(gameEvaluation.getAid());
        return article;
    }

    @Override
    public void addNewEvaluation(Game game, Integer score, String evaluationContent, User loginUser) {
        Article article = new Article();
        article.setPublisherid(loginUser.getUid());
        article.setLastmodifiedtime(new Date());
        articleMapper.insert(article);
        article.setArticleid(articleMapper.getLastArticleId());
        GameEvaluation gameEvaluation = new GameEvaluation();
        gameEvaluation.setAid(article.getArticleid());
        gameEvaluation.setGid(game.getGid());
        gameEvaluation.setDetail(evaluationContent);
        gameEvaluationMapper.insert(gameEvaluation);
        ArticleDetail articleDetail = new ArticleDetail();
        articleDetail.setAid(article.getArticleid());
        articleDetail.setDetail(evaluationContent);
        articleDetailMapper.insert(articleDetail);
    }

    @Override
    public boolean evaluationExist(Game game, User loginUser) {
        GameEvaluation gameEvaluation = gameEvaluationMapper.checkEvaluationExist(game, loginUser);
        return gameEvaluation != null;
    }
}
